# copy the log to a text file
sink("./lucid_replication2.txt")

# This file creates the correlations for the second column in Table 1 as well as the results in Table A6

# Load packages
library(dplyr)
library(tidyr)
library(psych)
library(car)
library(haven)

# Study 1 ####

# Read in data
dat <- read_dta("lucid_data2.dta")

# Merge in crosswalk data to identify what town/city respondents live income
cw <- read.csv("zipcrosswalk.csv")

dat <- merge(dat, cw, by="zip", all.x=T)

# Generate correlations by community type ####

dat %>% group_by(urbancity) %>% 
  summarise(cor=cor(natlscale, localscale, use="complete.obs"))

# Create within-municipality correlations
test <- statsBy(dat[,31:61], dat$placename, cors = T)

dat2 <- as.data.frame(test$rwg)

dat2$name1 <- rownames(dat2)

dat2 <- dat2 %>% pivot_longer(cols=c("affordable_housing.wg",
                                     "rent_controls.wg",
                                     "pre_education.wg",
                                     "public_transit.wg",
                                     "samesex_benefits.wg",
                                     "landuse_limits.wg",
                                     "aesthetic_impact.wg",
                                     "business_taxbreaks_1.wg",
                                     "business_taxbreaks_2.wg",
                                     "business_taxbreaks_3.wg",
                                     "increased_parking.wg",
                                     "require_recycling.wg",
                                     "increase_localpolice.wg",
                                     "employee_pension.wg",
                                     "internet_access.wg",
                                     "nimby.wg",
                                     "cut_socialservices.wg",
                                     "deficit_taxes.wg",
                                     "affirmative_action.wg",
                                     "environ_policy.wg",
                                     "gun_control.wg",
                                     "immigration.wg",
                                     "abortion.wg",
                                     "healthcare.wg",
                                     "cut_domestic_spend.wg",
                                     "cut_raise_taxes.wg",
                                     "tariffs_china.wg",
                                     "marijuana.wg",
                                     "military.wg",
                                     "environ_drill.wg",
                                     "voterid.wg"),
                              names_to="name2")

dat2 <- subset(dat2, name1!=name2)
dat2 <- dat2[!duplicated(dat2[c('value')]), ]

localissues <- c("affordable_housing.wg",
                 "rent_controls.wg",
                 "pre_education.wg",
                 "public_transit.wg",
                 "samesex_benefits.wg",
                 "landuse_limits.wg",
                 "aesthetic_impact.wg",
                 "business_taxbreaks_1.wg",
                 "business_taxbreaks_2.wg",
                 "business_taxbreaks_3.wg",
                 "increased_parking.wg",
                 "require_recycling.wg",
                 "increase_localpolice.wg",
                 "employee_pension.wg",
                 "internet_access.wg",
                 "nimby.wg",
                 "cut_socialservices.wg",
                 "deficit_taxes.wg")

dat2$venue1 <- "national"
dat2$venue2 <- "national"

condition <- dat2$name1 %in% localissues
dat2$venue1[condition] <- "local"
condition <- dat2$name2 %in% localissues
dat2$venue2[condition] <- "local"

dat2$abscor <- abs(dat2$value)

# Create average within community correlation
dat2 %>% group_by(venue1, venue2) %>%
  summarise(mean(abscor),
            sd.abscor = sd(abscor),
            n = n(),
            se.abscor = sd.abscor / sqrt(n))


sink()

